package com.siqi.hr.system.common.utils;

import com.siqi.hr.system.common.model.ModuleType;
import org.apache.commons.lang.StringUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;

public class Common {

    /**
	 * 获取引用字段后缀(索引)
	 * @author    xiaogq
	 * @param fieldName   引用字段名称
	 * @return
     */
	public static int getFieldSuffix(String fieldName) {
		if (fieldName.startsWith("pk_")) {
			String newfieldname = fieldName.replace("pk_t_", "");
			return Integer.valueOf(newfieldname.split("_")[1]);
		}
		return 1;
	}

    /**
	 * 根据引用字段名称解析出实体表名
	 * @param fieldName
	 * @return
     */
	public static String getFkTableNameByField(String fieldName) {
		if (fieldName.startsWith("pk_")) {
			String newfieldname = fieldName.replace("pk_", "");
			return newfieldname.substring(0, newfieldname.lastIndexOf("_"));
		}
		return fieldName;
	}

    /**
	 * 根据引用字段名称解析出实体id
	 * @param fieldName
	 * @return
     */
	public static int getTableCodeByFieldName(String fieldName) {
		int tableCode = 0;
		if (StringUtils.isNotBlank(fieldName) && fieldName.startsWith("pk_")){
			String newfieldname = fieldName.replace("pk_t_", "");
			tableCode = Integer.valueOf(newfieldname.split("_")[0]);
		}
		return tableCode;
	}

	/**
	 * 处理角色列表可登录模块字段方法(也就是把可登录模块的id转换成名称)
	 * @param loginModule
	 * @return
	 */
	public static String tranformLoginModule(String loginModule) {
		StringBuilder modulesStr = new StringBuilder();
		if(StringUtils.isNotBlank(loginModule)){
			Collection<ModuleType> modules = EnumUtils.tranform(loginModule, ModuleType.class);
			if (modules.size() > 0) {
				for (ModuleType mod : modules) {
					String modDesc = mod.getDesc();
					if ((modDesc != null) && (!modDesc.equals("")))
						modulesStr.append(mod.getDesc() + ",");
				}
			}
			if (!modulesStr.toString().equals(""))
				modulesStr.deleteCharAt(modulesStr.lastIndexOf(","));
		}
		return modulesStr.toString();
	}

	/**
	 * 密码进行Md5加密
	 * @param plainText
	 * @return
     */
	public static String Md5(String plainText) {
		if (plainText == null)
			plainText = "";
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte[] b = md.digest();

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				int i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}

			return buf.toString();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}

    /**
	 * 添加员工档案时获取员工工号
	 * @author     xiaogq
	 * @param maxJobNumber
	 * @return
     */
	public static String getMaxJobNumber(int maxJobNumber){
		String jobNumber = "CS0001";
		if(0 < maxJobNumber && maxJobNumber < 10){
			jobNumber = "CS000"+maxJobNumber;
		}else if(maxJobNumber >= 10 && maxJobNumber < 100){
			jobNumber = "CS00"+maxJobNumber;
		}else if(maxJobNumber >= 100 && maxJobNumber < 1000){
			jobNumber = "CS0"+maxJobNumber;
		}else if(maxJobNumber >= 1000){
			jobNumber = "CS"+maxJobNumber;
		}
		return jobNumber;
	}

	public static void main(String [] args){
		String bb = getMaxJobNumber(3);
		System.out.println("bb==="+bb);
	}

}
